﻿using AdminStystem.Domain;
using AdminSytem.Application.Dto;
using AdminSytem.Repositories;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminSytem.Application.Traceabilities
{
    public class TraceabilityServices : ITraceabilityServices
    {
        public readonly IRepository<Traceability> repository;
        public readonly IRepository<Goods> goodsrepository;
        public readonly IMapper mapper;

        public TraceabilityServices(IRepository<Traceability> repository, IRepository<Goods> goodsrepository, IMapper mapper)
        {
            this.repository = repository;
            this.goodsrepository = goodsrepository;
            this.mapper = mapper;
        }
        public async Task<ResultDto<int>> InsertTraceability(InsertTraceabilityDto dto)
        {
            ResultDto<int> result = new ResultDto<int>();
            result.StatusCode = StatusCode.Success;
            result.Message = "添加成功";
            var entity = mapper.Map<Traceability>(dto);

            await repository.InsertAsync(entity);
            return result;
        }
        /// <summary>
        /// 查询溯源信息
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<ApiPaiing<TraceabilityDto>>> GetTraceability(int index, int size)
        {
            ResultDto<ApiPaiing<TraceabilityDto>> result = new ResultDto<ApiPaiing<TraceabilityDto>>();
            result.StatusCode = StatusCode.Success;
            result.Message = "查询成功";
            var list = from a in await repository.QueryAsync()
                       join b in await goodsrepository.QueryAsync()
                       on a.GoodsId equals b.GoodsId
                       where a.IsDeleted==true
                       select new TraceabilityDto
                       {
                           TraceabilityId= a.TraceabilityId,
                           GoodsId = a.GoodsId,
                           GoodsName = b.GoodsName,
                           Produce = a.Produce,
                           SafetyDay = a.SafetyDay,
                           LicenseNumber = a.LicenseNumber,
                           Enterprise = a.Enterprise,
                           InformationComp = a.InformationComp,
                           Phtoto = a.Phtoto
                       };
            ApiPaiing<TraceabilityDto> page = new ApiPaiing<TraceabilityDto>();
            page.totalcount = list.Count();
            page.totalpage = (int)Math.Ceiling(page.totalcount * 1.0 / size);
            page.List = list.Skip(size * (index - 1)).Take(size).ToList();
            result.Data = page;
            return result;
        }
        public async Task<ResultDto<List<TraceabilityDto>>> Gettraceability(int id)
        {
            ResultDto<List<TraceabilityDto>> result = new ResultDto<List<TraceabilityDto>>();
            result.StatusCode = StatusCode.Success;
            result.Message = "查询成功";
            var list = from a in await repository.QueryAsync()
                       join b in await goodsrepository.QueryAsync()
                       on a.GoodsId equals b.GoodsId
                       where a.TraceabilityId == id
                       select new TraceabilityDto
                       {
                           TraceabilityId = a.TraceabilityId,
                           GoodsId = a.GoodsId,
                           GoodsName = b.GoodsName,
                           Produce = a.Produce,
                           SafetyDay = a.SafetyDay,
                           LicenseNumber = a.LicenseNumber,
                           Enterprise = a.Enterprise,
                           InformationComp = a.InformationComp,
                           Phtoto = a.Phtoto
                       };
           
            result.Data = list.ToList();
            return result;
        }
        /// <summary>
        /// 查询商品信息
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<List<Goods>>> GetGoods()
        {
            ResultDto<List<Goods>> result = new ResultDto<List<Goods>>();
            result.StatusCode = StatusCode.Success;
            result.Message = "查询成功";
            var list = await goodsrepository.QueryAsync();
            result.Data = list;
            return result;
        } 
        public async Task<ResultDto<List<Traceability>>>GetOnly(int id)
        {
            ResultDto<List<Traceability>> result = new ResultDto<List<Traceability>>();
            result.StatusCode = StatusCode.Success;
            result.Message = "查询成功";
            var list = await repository.QueryAsync(a => a.TraceabilityId == id);
            result.Data = list;
            return result;
        }
        public async Task<ResultDto<Traceability>> Deltraceability(int id)
        {
            ResultDto<Traceability> result = new ResultDto<Traceability>();
            result.StatusCode = StatusCode.Success;
            result.Message = "删除成功";
            var list = await repository.QueryFirstAsync(a=>a.TraceabilityId==id);
           list.IsDeleted = false;
            var entity = repository.UpdateAsync(list);
            result.Data = list;
            return result;
        }
        public async Task<ResultDto<int>> UpdateTraceability(UpdateTraceabilityDto dto)
        {
            ResultDto<int> result = new ResultDto<int>();
            result.StatusCode = StatusCode.Success;
            result.Message = "修改成功";
            var entity = mapper.Map<Traceability>(dto);
            entity.LicenseNumber = "40%";
            entity.IsDeleted = true;
            var res= await repository.UpdateAsync(entity);

            result.Data = res;

            return result;
        }
    }
}
